import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText




class Mail:

    def __init__(self):
        self.smtp = smtplib.SMTP('smtp.qq.com', 587)
        self.smtp.starttls()  # 启用 TLS 加密连接
        # self.smtp.ehlo()
        # self.smtp.starttls()
        # self.smtp.connect('smtp.exmail.qq.com', 465)
        self.smtp.login('195505183@qq.com', 'mqjcmynbbkvfbicf')

    def send(self, subject, content, to, msg):
        msg['Subject'] = subject
        msg['from'] = '195505183@qq.com'
        # msg['To'] = to
        msg.attach(MIMEText(content, 'plain', 'utf-8'))
        # self.smtp.sendmail('tanfeng001@yuzj010.com', to, msg.as_string())
        self.smtp.sendmail('195505183@qq.com', to, msg.as_string())


if __name__ == "__main__":
    file = "c:/nginx.conf"
    attach = MIMEBase('application', 'octet-stream')
    attach.set_payload(open(file, 'rb').read())
    attach.add_header('Content-Type', 'application/octet-stream')
    attach.add_header('Content-Disposition', 'attachment', filename='测试附件.txt')
    encoders.encode_base64(attach)

    msg = MIMEMultipart()
    msg.attach(attach)

    mail = Mail()
    mail.send('测试标题', '测试内容', '596744967@qq.com', msg)
